package com.hlyj.http.base.tool.lib_hlyj_base.utils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hlyj.http.base.tool.lib_hlyj_base.bean.ConfigsDTO;
import com.hlyj.http.base.tool.lib_hlyj_base.bean.DictsDTO;


import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

public class DataUtils {

    String dicts = "dicts";

    String configs = "configs";

    String udi = "udi";

    String type = "type";

    private static String open = "open";

    private static String adLimit = "adDayLimit";

    private static String LIMIT_ARPU = "sigtArpu";

    private static String SER_POP_GAP = "serPopGap";

    private static DataUtils instance;

    private Gson gson = new Gson();

    public static DataUtils getInstance() {
        if (instance == null) {
            synchronized (DataUtils.class) {
                if (instance == null) {
                    instance = new DataUtils();
                }
            }
        }
        return instance;
    }

    public String getUdi() {
        return BaseModuleSpUtils.getInstance().getString(udi);
    }

    private void setUdi(String udi_) {
        BaseModuleSpUtils.getInstance().putString(udi, udi_);
    }

    public boolean getOpen() {
        int result = BaseModuleSpUtils.getInstance().getInt(open, -1);
        return result == 0;
    }

    public int getAdDayLimit(int deLimit){
        return BaseModuleSpUtils.getInstance().getInt(adLimit, deLimit);
    }


    public void createUdi() {
        String udi = getUdi();
        if (udi == null || udi.isEmpty()) {
            setUdi(Tool.createUdi());
        }
    }

    public double getConfig(String key) {
        List<ConfigsDTO> configList = gson.fromJson(BaseModuleSpUtils.getInstance().getString(configs), new TypeToken<List<ConfigsDTO>>() {
        }.getType());
        List<ConfigsDTO> list = configList.stream().filter(item -> item.getPtCode().equals(key)).collect(Collectors.toList());
        return list.size() != 0 ? list.stream().findFirst().get().getDeductScale() : 0;
    }

    public void setConfig(List<ConfigsDTO> configList) {
        BaseModuleSpUtils.getInstance().putString(configs, gson.toJson(configList));
    }

    public void putConfig(String key,int value){
        BaseModuleSpUtils.getInstance().putInt(key, value);
    }

    public void putConfig(String key,float value){
        BaseModuleSpUtils.getInstance().putFloat(key, value);
    }

    /**
     * 获取单用户限制 ARPU，超过之后不再弹出，默认 3
     * @return
     */
    public float getLimitArpu(){
        float limitArpu = BaseModuleSpUtils.getInstance().getFloat(LIMIT_ARPU, 3f);
        return limitArpu;
    }

    /**
     * 获取单用户弹出间隔
     * @return
     */
    public int getPopGap(){
        int gap = BaseModuleSpUtils.getInstance().getInt(SER_POP_GAP);
        return gap * 1000;// 转化成秒
    }

    public String getDicts(String key) {
        List<DictsDTO> configList = gson.fromJson(BaseModuleSpUtils.getInstance().getString(dicts), new TypeToken<List<DictsDTO>>() {
        }.getType());
        List<DictsDTO> list = configList.stream().filter(item -> item.getK().equals(key)).collect(Collectors.toList());
        return list.size() != 0 ? list.stream().findFirst().get().getV() : "";
    }

    public void setDicts(List<DictsDTO> configList) {
        BaseModuleSpUtils.getInstance().putString(dicts, gson.toJson(configList));
    }

    public boolean isDeviceUpSucc() {
        return BaseModuleSpUtils.getInstance().getBoolean("deviceUpSucc", false);
    }

    public void setDeviceUpSucc() {
        BaseModuleSpUtils.getInstance().putBoolean("deviceUpSucc", true);
    }

    public void saveFirstStartTime() {
        BaseModuleSpUtils.getInstance().putLong("firstStartTime", System.currentTimeMillis());
    }

    public long getFirstStartTime() {
        return BaseModuleSpUtils.getInstance().getLong("firstStartTime");
    }

    public void saveAppStartFlag() {
        int oldFlag = getAppStartFlag();
        if (oldFlag == -1) {
            oldFlag = 0;
            saveFirstStartTime();

            BaseModuleSpUtils.getInstance().putInt("appStartFlag", oldFlag);
            return;
        }
        long last = getFirstStartTime();
        long now = System.currentTimeMillis();
        // 判断 now 是否大于 last，按照自然日计算, flag == 2 为次日启动
        if (TimeTools.INSTANCE.isNextDay(last, now) && oldFlag != 2) {
            oldFlag = 2;
        } else {
            oldFlag = 1;
        }
        BaseModuleSpUtils.getInstance().putInt("appStartFlag", oldFlag);
    }

    public int getAppStartFlag() {
        return BaseModuleSpUtils.getInstance().getInt("appStartFlag");
    }

    public int getAdShowNum() {
        return BaseModuleSpUtils.getInstance().getInt("adShowNum");
    }

    public void updateAdShowNum(int newValue) {
        BaseModuleSpUtils.getInstance().putInt("adShowNum", newValue);
    }

    public double getAdEpcm() {
        return BaseModuleSpUtils.getInstance().getFloat("adEpcm");
    }

    public void updateAdEpcm(double newValue) {
        BaseModuleSpUtils.getInstance().putFloat("adEpcm", (float) newValue);
    }

    /**
     * 保存 float 广告总价值
     * @param bigDecimal
     */
    public void setFAdEcpm(BigDecimal bigDecimal){
        try {
            float fecpm = bigDecimal.floatValue();
            BaseModuleSpUtils.getInstance().putFloat("fAdEcpm",fecpm);
        }catch (Exception e){
            //LLogs.INSTANCE.e("setFAdEcpm 出错","data utils");
        }
    }

    public float getFAdEcpm(){
        return BaseModuleSpUtils.getInstance().getFloat("fAdEcpm");
    }


    public int getRewardVideoAd() {
        return BaseModuleSpUtils.getInstance().getInt("VideoAd");
    }

    public void setRewardVideoAd() {
        int amount = getRewardVideoAd();
        amount = amount + 1;
        if (amount == 12) {
            amount = 0;
            setType(true);
        }
        BaseModuleSpUtils.getInstance().putInt("VideoAd", amount);
    }

    public int getScreenVideoAd() {
        return BaseModuleSpUtils.getInstance().getInt("ScreenAd");
    }

    public void setScreenVideoAd() {
        int amount = getScreenVideoAd();
        amount = amount + 1;
        if (amount == 12) {
            amount = 0;
            setType(false);
        }
        BaseModuleSpUtils.getInstance().putInt("ScreenAd", amount);
    }


    public boolean getType() {
        return BaseModuleSpUtils.getInstance().getBoolean("type");
    }

    public void setType(boolean type) {
        BaseModuleSpUtils.getInstance().putBoolean("type", type);
    }

    public boolean getEnter() {
        return BaseModuleSpUtils.getInstance().getBoolean("Enter");
    }

    public boolean isAgreedAgreement(){
        return BaseModuleSpUtils.getInstance().getBoolean("app_frist");
    }

    public void setEnter(boolean enter) {
        BaseModuleSpUtils.getInstance().putBoolean("Enter", enter);
    }

    // 保存当天广告弹出次数，以每日零点作为 key
    public void updateToDayAdShowNum(){
        int num = getToDayShowNum()+1;
        String key = TimeTools.INSTANCE.getTodayZeroTime()+"";
        BaseModuleSpUtils.getInstance().putInt(key, num);
    }

    // 获取当天广告弹出次数
    public int getToDayShowNum(){
        String key = TimeTools.INSTANCE.getTodayZeroTime()+"";
        return BaseModuleSpUtils.getInstance().getInt(key);
    }
}
